package com.kaichaohulian.baocms.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.kaichaohulian.baocms.R;
import com.kaichaohulian.baocms.adapter.SearchAdapter;
import com.kaichaohulian.baocms.adapter.SearchPopAdapter;
import com.kaichaohulian.baocms.base.BaseMyActivity;
import com.kaichaohulian.baocms.http.HttpUtil;
import com.kaichaohulian.baocms.http.Url;
import com.kaichaohulian.baocms.newmodel.SearchModel;
import com.kaichaohulian.baocms.utils.DBLog;
import com.kaichaohulian.baocms.view.ShowDialog;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SearchActivity extends BaseMyActivity {


    @BindView(R.id.iv_back)
    ImageView ivBack;

    @BindView(R.id.exit_layout)
    LinearLayout exitLayout;
    @BindView(R.id.center_title_tv)
    TextView centerTitleTv;
    @BindView(R.id.iv_image1)
    ImageView ivImage1;
    @BindView(R.id.navigation_bar)
    RelativeLayout navigationBar;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    String word = "";
    String type;
    int page = 1;
    List<SearchModel.DataObjectBean> searchList;
    SearchAdapter searchAdapter;
    private PopupWindow popLeft;
    SearchPopAdapter searchPopAdapter;
    List<String> list = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        ButterKnife.bind(this);

        searchList = new ArrayList<>();
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        searchAdapter = new SearchAdapter(searchList);
        searchAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);
        searchAdapter.isFirstOnly(false);
        recyclerView.setAdapter(searchAdapter);
        getSearchData();
        list.add("帮我买");
        list.add(" 帮我取");
        list.add("帮我送");
        list.add("发布服务");

        list.add("排队");
        list.add("其他需求");
        list.add("顺风车");
        list.add("异地代购");

        list.add("预约");

        list.add("共享闲置");
        searchPopAdapter = new SearchPopAdapter(list);
    }


    public void getSearchData() {
        type = getIntent().getStringExtra("type");
        word = getIntent().getStringExtra("word");
        final RequestParams params = new RequestParams();
        params.put("word", word);
        params.put("type", type);
        params.put("page", 1);
//        Log.d(Url.search_url + ? word = 0 + ? type = 1 + ? page = 1, "3333333333333333333333333");
        HttpUtil.post(Url.search_url, params, new JsonHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                Log.d(response.toString(), "333333333333333333");
                Gson gson = new Gson();
                SearchModel searchMode = new SearchModel();
                searchMode = gson.fromJson(response.toString(), SearchModel.class);
//                Toast.makeText(SearchActivity.this, "成功", Toast.LENGTH_SHORT).show();
                if (searchList.isEmpty()) {

                    searchList = searchMode.getDataObject();
                    searchAdapter.setNewData(searchList);
                } else {
                    searchList.clear();
                    searchList = searchMode.getDataObject();
                    searchAdapter.setNewData(searchList);

                }


            }

            @Override
            public void onFinish() {
                ShowDialog.dissmiss();
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                DBLog.e("tag", statusCode + ":" + responseString);
                ShowDialog.dissmiss();
                Toast.makeText(SearchActivity.this, "失败", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancel() {
//                Toast.makeText(getActivity(), "请求取消", Toast.LENGTH_SHORT).show();
                ShowDialog.dissmiss();
            }
        });

    }

    private View layoutLeft;

    @OnClick({R.id.iv_back, R.id.exit_layout, R.id.center_title_tv, R.id.iv_image1, R.id.navigation_bar})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;

            case R.id.exit_layout:
                break;
            case R.id.center_title_tv:
                break;
            case R.id.iv_image1:
                Toast.makeText(this, "搜索呢", Toast.LENGTH_SHORT).show();
//                showPopupWindow(ivImage1);

                showpopWindow();

                break;
            case R.id.navigation_bar:
                break;
        }
    }

    public void showpopWindow() {


        if (popLeft != null && popLeft.isShowing()) {
            popLeft.dismiss();
        } else {
            layoutLeft = getLayoutInflater().inflate(
                    R.layout.pop_menulist, null);

            popLeft = new PopupWindow(layoutLeft, navigationBar.getWidth(),
                    RelativeLayout.LayoutParams.WRAP_CONTENT);


//            RecyclerView recyclerView = (RecyclerView) popLeft.findViewById(R.id.recycle_shuju);

            RecyclerView recyclerView = (RecyclerView) layoutLeft
                    .findViewById(R.id.recycle_shuju);
            final EditText edit_search = (EditText) layoutLeft.findViewById(R.id.edit_search);


            recyclerView.setLayoutManager(new LinearLayoutManager(SearchActivity.this));


            //帮我取
            TextView text_help_get = (TextView) layoutLeft.findViewById(R.id.text_help_get);
            final ImageView img_a1 = (ImageView) layoutLeft.findViewById(R.id.img_a1);


            //帮我买
            TextView text_help_mai = (TextView) layoutLeft.findViewById(R.id.text_help_mai);
            final ImageView img_a2 = (ImageView) layoutLeft.findViewById(R.id.img_a2);
            //帮我送
            TextView text_help_song = (TextView) layoutLeft.findViewById(R.id.text_help_song);
            final ImageView img_a3 = (ImageView) layoutLeft.findViewById(R.id.img_a3);
            //异地代购
            TextView text_help_yidi = (TextView) layoutLeft.findViewById(R.id.text_help_yidi);
            final ImageView img_a4 = (ImageView) layoutLeft.findViewById(R.id.img_a4);
            //排队
            TextView text_help_paidui = (TextView) layoutLeft.findViewById(R.id.text_help_paidui);
            final ImageView img_a5 = (ImageView) layoutLeft.findViewById(R.id.img_a5);
            //预约
            TextView text_help_yuyue = (TextView) layoutLeft.findViewById(R.id.text_help_yuyue);
            final ImageView img_a6 = (ImageView) layoutLeft.findViewById(R.id.img_a6);
            //顺风车
            TextView text_help_shunfeng = (TextView) layoutLeft.findViewById(R.id.text_help_shunfeng);
            final ImageView img_a7 = (ImageView) layoutLeft.findViewById(R.id.img_a7);
            //其他服务
            TextView text_help_other = (TextView) layoutLeft.findViewById(R.id.text_help_other);
            final ImageView img_a8 = (ImageView) layoutLeft.findViewById(R.id.img_a8);


//发布服务
            TextView text_help_fuwu = (TextView) layoutLeft.findViewById(R.id.text_help_fuwu);
            final ImageView img_a9 = (ImageView) layoutLeft.findViewById(R.id.img_a9);
//共享闲置
            TextView text_help_gongxiang = (TextView) layoutLeft.findViewById(R.id.text_help_gongxiang);
            final ImageView img_a10 = (ImageView) layoutLeft.findViewById(R.id.img_a10);
            word = edit_search.getText().toString();
//帮我取
            text_help_get.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    type = "2";

                    img_a1.setVisibility(View.VISIBLE);
                    img_a2.setVisibility(View.GONE);
                    img_a3.setVisibility(View.GONE);
                    img_a4.setVisibility(View.GONE);
                    img_a5.setVisibility(View.GONE);
                    img_a6.setVisibility(View.GONE);
                    img_a7.setVisibility(View.GONE);
                    img_a8.setVisibility(View.GONE);
                    img_a9.setVisibility(View.GONE);
                    img_a10.setVisibility(View.GONE);

                }
            });

            //帮我买
            text_help_mai.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    type = "1";

                    img_a1.setVisibility(View.GONE);
                    img_a2.setVisibility(View.VISIBLE);
                    img_a3.setVisibility(View.GONE);
                    img_a4.setVisibility(View.GONE);
                    img_a5.setVisibility(View.GONE);
                    img_a6.setVisibility(View.GONE);
                    img_a7.setVisibility(View.GONE);
                    img_a8.setVisibility(View.GONE);
                    img_a9.setVisibility(View.GONE);
                    img_a10.setVisibility(View.GONE);
                }
            });

//帮我送
            text_help_song.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    type = "3";

                    img_a1.setVisibility(View.GONE);
                    img_a2.setVisibility(View.GONE);
                    img_a3.setVisibility(View.VISIBLE);
                    img_a4.setVisibility(View.GONE);
                    img_a5.setVisibility(View.GONE);
                    img_a6.setVisibility(View.GONE);
                    img_a7.setVisibility(View.GONE);
                    img_a8.setVisibility(View.GONE);
                    img_a9.setVisibility(View.GONE);
                    img_a10.setVisibility(View.GONE);
                }
            });

//发布服务
            text_help_fuwu.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    type = "4";

                    img_a1.setVisibility(View.GONE);
                    img_a2.setVisibility(View.GONE);
                    img_a3.setVisibility(View.GONE);
                    img_a4.setVisibility(View.GONE);
                    img_a5.setVisibility(View.GONE);
                    img_a6.setVisibility(View.GONE);
                    img_a7.setVisibility(View.GONE);
                    img_a8.setVisibility(View.GONE);
                    img_a9.setVisibility(View.VISIBLE);
                    img_a10.setVisibility(View.GONE);
                }
            });
//共享闲置
            text_help_gongxiang.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    type = "10";

                    img_a1.setVisibility(View.GONE);
                    img_a2.setVisibility(View.GONE);
                    img_a3.setVisibility(View.GONE);
                    img_a4.setVisibility(View.GONE);
                    img_a5.setVisibility(View.GONE);
                    img_a6.setVisibility(View.GONE);
                    img_a7.setVisibility(View.GONE);
                    img_a8.setVisibility(View.GONE);
                    img_a9.setVisibility(View.GONE);
                    img_a10.setVisibility(View.VISIBLE);
                }
            });

//异地代购
            text_help_yidi.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    type = "8";

                    img_a1.setVisibility(View.GONE);
                    img_a2.setVisibility(View.GONE);
                    img_a3.setVisibility(View.GONE);
                    img_a4.setVisibility(View.VISIBLE);
                    img_a5.setVisibility(View.GONE);
                    img_a6.setVisibility(View.GONE);
                    img_a7.setVisibility(View.GONE);
                    img_a8.setVisibility(View.GONE);
                    img_a9.setVisibility(View.GONE);
                    img_a10.setVisibility(View.GONE);
                }
            });
//排队
            text_help_paidui.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    type = "5";

                    img_a1.setVisibility(View.GONE);
                    img_a2.setVisibility(View.GONE);
                    img_a3.setVisibility(View.GONE);
                    img_a4.setVisibility(View.GONE);
                    img_a5.setVisibility(View.VISIBLE);
                    img_a6.setVisibility(View.GONE);
                    img_a7.setVisibility(View.GONE);
                    img_a8.setVisibility(View.GONE);
                    img_a9.setVisibility(View.GONE);
                    img_a10.setVisibility(View.GONE);
                }
            });

//预约
            text_help_yuyue.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    type = "9";

                    img_a1.setVisibility(View.GONE);
                    img_a2.setVisibility(View.GONE);
                    img_a3.setVisibility(View.GONE);
                    img_a4.setVisibility(View.GONE);
                    img_a5.setVisibility(View.GONE);
                    img_a6.setVisibility(View.VISIBLE);
                    img_a7.setVisibility(View.GONE);
                    img_a8.setVisibility(View.GONE);
                    img_a9.setVisibility(View.GONE);
                    img_a10.setVisibility(View.GONE);
                }
            });

//顺风车
            text_help_shunfeng.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    type = "7";

                    img_a1.setVisibility(View.GONE);
                    img_a2.setVisibility(View.GONE);
                    img_a3.setVisibility(View.GONE);
                    img_a4.setVisibility(View.GONE);
                    img_a5.setVisibility(View.GONE);
                    img_a6.setVisibility(View.GONE);
                    img_a7.setVisibility(View.VISIBLE);
                    img_a8.setVisibility(View.GONE);
                    img_a9.setVisibility(View.GONE);
                    img_a10.setVisibility(View.GONE);
                }
            });
//其他
            text_help_other.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    type = "6";

                    img_a1.setVisibility(View.GONE);
                    img_a2.setVisibility(View.GONE);
                    img_a3.setVisibility(View.GONE);
                    img_a4.setVisibility(View.GONE);
                    img_a5.setVisibility(View.GONE);
                    img_a6.setVisibility(View.GONE);
                    img_a7.setVisibility(View.GONE);
                    img_a8.setVisibility(View.VISIBLE);
                    img_a9.setVisibility(View.GONE);
                    img_a10.setVisibility(View.GONE);
                }
            });

            Button btn_cancel = (Button) layoutLeft.findViewById(R.id.btn_cancel);
            btn_cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    popLeft.dismiss();
                }
            });
            Button btn_submit = (Button) layoutLeft.findViewById(R.id.btn_submit);
            btn_submit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    getSearchData();

                    final RequestParams params = new RequestParams();
                    params.put("word", word);
                    params.put("type", type);
                    params.put("page", 1);
//        Log.d(Url.search_url + ? word = 0 + ? type = 1 + ? page = 1, "3333333333333333333333333");
                    HttpUtil.post(Url.search_url, params, new JsonHttpResponseHandler() {
                        @Override
                        public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                            Log.d(response.toString(), "333333333333333333");
                            Gson gson = new Gson();
                            SearchModel searchMode = new SearchModel();
                            searchMode = gson.fromJson(response.toString(), SearchModel.class);
//                Toast.makeText(SearchActivity.this, "成功", Toast.LENGTH_SHORT).show();
                            if (searchList.isEmpty()) {

                                searchList = searchMode.getDataObject();
                                searchAdapter.setNewData(searchList);
                            } else {
                                searchList.clear();
                                searchList = searchMode.getDataObject();
                                searchAdapter.setNewData(searchList);

                            }


                        }

                        @Override
                        public void onFinish() {
                            ShowDialog.dissmiss();
                        }

                        @Override
                        public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                            DBLog.e("tag", statusCode + ":" + responseString);
                            ShowDialog.dissmiss();
                            Toast.makeText(SearchActivity.this, "失败", Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        public void onCancel() {
//                Toast.makeText(getActivity(), "请求取消", Toast.LENGTH_SHORT).show();
                            ShowDialog.dissmiss();
                        }
                    });


                    popLeft.dismiss();
                }
            });


            recyclerView.setAdapter(searchPopAdapter);
      /* searchAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    Toast.makeText(SearchActivity.this, "呵呵哒", Toast.LENGTH_SHORT).show();

                    type = position + 1 + "";
                    word = edit_search.getText().toString().trim();
                }
            });*/
          /*  searchAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
                @Override
                public boolean onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                    switch (view.getId()){
                        case R.id.item_pop:

                            type = position + 1 + "";
                            word = edit_search.getText().toString().trim();
                            Toast.makeText(SearchActivity.this, type+"", Toast.LENGTH_SHORT).show();
                            Toast.makeText(SearchActivity.this, word+"", Toast.LENGTH_SHORT).show();
                            break;
                    }

                    return true;
                }
            });*/

            searchAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
                @Override
                public boolean onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                    type = position + 1 + "";
                    word = edit_search.getText().toString().trim();
                    Toast.makeText(SearchActivity.this, type + "", Toast.LENGTH_SHORT).show();
                    Toast.makeText(SearchActivity.this, word + "", Toast.LENGTH_SHORT).show();

                    return false;
                }
            });
            popLeft.setAnimationStyle(R.style.PopupAnimation);
            popLeft.update();
            popLeft.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
            popLeft.setTouchable(true); // 设置popupwindow可点击
            popLeft.setOutsideTouchable(true); // 设置popupwindow外部可点击
            popLeft.setFocusable(true); // 获取焦点

            popLeft.showAsDropDown(navigationBar, 0, 0);
            popLeft.setTouchInterceptor(new View.OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    // 如果点击了popupwindow的外部，popupwindow也会消失
                    if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                        popLeft.dismiss();
                        return true;
                    }
                    return false;
                }
            });
        }
    }

}
